package com.hexanet.server.dao;

import org.hibernate.Session;

import com.hexanet.bom.Player;
import com.hexanet.server.hibernate.HibernateUtil;

public class PlayerDao extends DataAccessDao<Player> {

    public Player getByPrincipal(String mail) {
	Session session = getSession();

	Player p = (Player) session
		.createQuery("Select p from Player p where mail = :MAIL")
		.setParameter("MAIL", mail).uniqueResult();

	return p;
    }

    public Player login(String mail, String passwordSha1) {
	Session session = HibernateUtil.getSession();

	Player p = (Player) session
		.createQuery(
			"Select p from Player p where mail = :MAIL and passwordSha1 = :PASSWORD")
		.setParameter("MAIL", mail)
		.setParameter("PASSWORD", passwordSha1).uniqueResult();

	return p;
    }

    public boolean playerExists(String mail) {

	Session session = HibernateUtil.getSession();

	Player p = (Player) session
		.createQuery("Select p from Player p where mail = :MAIL")
		.setParameter("MAIL", mail).uniqueResult();

	// if there is a player with mail
	if (p != null) {
	    return true;
	}

	return false;

    }

}
